home *** CD-ROM | disk | FTP | other *** search
- Path: hoxey.torolab.ibm.com!hoxey
- From: hoxey@hoxey.torolab.ibm.com ()
- Newsgroups: comp.lang.c
- Subject: Re: Is This Bad Coding Practice?
- Date: 29 Mar 1996 20:05:02 GMT
- Organization: IBM Toronto
- Distribution: world
- Message-ID: <4jhfpe$15n2@tornews.torolab.ibm.com>
- References: <4jgnt2$9d1@loki.tor.hookup.net>
- Reply-To: hoxey@vnet.ibm.com
- NNTP-Posting-Host: hoxey.torolab.ibm.com
-
- In article <4jgnt2$9d1@loki.tor.hookup.net>, Rajendra_Singh@msn.com (Rajendra Singh) writes:
- |> int main(int argc, char *argv[])
- |> {
- |> char string[128];
- |> char *func1(void);
- |>
- |> strcpy(string, func1());
- |> return 0;
- |> }
- |>
- |> char *func1(void)
- |> {
- |> char test[100];
- |>
- |> sprintf(test, "Test: %d", 1);
- |> return test;
- |> }
- |>
- |> ... since I am using the value returned from func1() immediately (in
- |> main()), is this reliable? After I copy it into "string", I won't be
- Most definitely NOT!
-
- |> using that area of memory anymore (i. e. the pointer returned by
- |> func1()).
- What if:
- a) strcpy happens to be a *true* function (as opposed to a builtin) which
- grabs some stack space for temporary use?... typically it would re-use
- the space abandoned by func1()...
- OR b) immediately on exit from func1() your process loses use of the
- processor due to an interrupt or something? Even if strcpy is a
- builtin it is likely not an atomic operation... On some systems the
- OS will use your stack at various points along the path to servicing
- who know's what...
-
- When func1() returns, the local 'char test[100];' is no more!
-
- Steve
-